#ifndef TRANSPORT_CLASS
#define TRANSPORT_CLASS
#include <pland.hpp>
#include <corechain.hpp>
#include <tinyxml.h>
#include <vector>
#include <map>
#include <list>
#include <string>
#include <boost/thread.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/shared_mutex.hpp>
#include "routingtable.hpp"
#include "workitem.hpp"

namespace kport
{
class Transport
{
public:
    Transport(std::string config);
    ~Transport();
    void loadQueues(std::vector<pland::Chain::Chain_Queue> queues);
    bool startListner();
    bool stopListner();
    bool connectOutbound();
    bool start();
    bool stop();
    std::list <kport::WorkItemStat> getTraceBack();
    uint getAvgLatency();
    uint getAvgThroughput();
private:

    static const int NAME_MAX_LENGTH = 40;
    static const int TRACE_BACK_LENGTH = 1000;

    std::map <std::string, pland::Chain::Chain_Queue > queues_;
    typedef std::pair<boost::mutex*, WorkItem*> WorkerDescriptor;
    std::vector < WorkerDescriptor > workItems_;
    boost::shared_mutex workerManipulator_;

    struct TransportRules
    {
        int k;
        int bandwidth;
    };

    bool connectRemote_(std::string netName);

    uint port_;
    TransportRules rules_;
    void loadSettings_(std::string config_xml);

    bool running_;
    bool listening_;

    boost::thread listener_;
    boost::thread_group workers_;

    void workerThread_();
    void listenerThread_();

    boost::circular_buffer<WorkItemStat> transportStatistics_;
    boost::mutex statisticsLock_;
};

}

#endif
